package manager.impl;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;

import parser.HostFileParser;
import properties.PropertyManager;
import startup.StartupManager;

import RMI.adminTOrtdg.impl.Sender;
import RMI.rtdgTOadmin.impl.Receiver;
import RTDGDiscovery.DiscoveryDispatcher;
import RTDGDiscovery.DiscoveryListener;

import enums.AdminRequestType;

import host.impl.Host;
import host.impl.HostTableModel;
import logs.LogTableModel;
import manager.IServiceManager;

public class ServiceManager implements IServiceManager{

	private ArrayList<Host> hosts;
	public static DiscoveryListener discoveryListener;
	public static DiscoveryDispatcher discoveryDispatcher;
	public static Receiver rmiReceiver;
	public static Sender rmiSender;
	public static String name;
	private boolean automaticDiscover;
	public static String[] comboBoxDataProvider;
	private int comboBoxDataProviderSize;
	public static HostTableModel table = new HostTableModel();
	public static LogTableModel logTable = new LogTableModel();
			
	public String[] getComboboxDataProvider() {
		return comboBoxDataProvider;
	}
	
	public static void printLog(String txt){
		System.out.println(txt);
		logTable.addLog(txt);
	}
	
	public int hostNumber(){
		return hosts.size();
	}

	public void setComboBoxDataProviderSize(int comboBoxDataProviderSize) {
		this.comboBoxDataProviderSize = comboBoxDataProviderSize;
	}
	
	public ArrayList<Host> getHosts() {
		return hosts;
	}

	public void setHosts(ArrayList<Host> hosts) {
		this.hosts = hosts;
	}

	public void setDiscoveryListener(DiscoveryListener discoveryListener) {
		ServiceManager.discoveryListener = discoveryListener;
	}

	public void setDiscoveryDispatcher(DiscoveryDispatcher discoveryDispatcher) {
		ServiceManager.discoveryDispatcher = discoveryDispatcher;
	}

	public void setRmiReceiver(Receiver rmiReceiver) {
		ServiceManager.rmiReceiver = rmiReceiver;
	}

	public void setRmiSender(Sender rmiSender) {
		ServiceManager.rmiSender = rmiSender;
	}
	
	public void setName(String name) {
		ServiceManager.name = name;
	}

	public boolean isAutomaticDiscover() {
		return automaticDiscover;
	}

	public void setAutomaticDiscover(boolean automaticDiscover) {
		this.automaticDiscover = automaticDiscover;
	}

	public ServiceManager() {
	}

	public void initServices() {
		if(automaticDiscover){
				discover();
		}
		hosts = new ArrayList<Host>();
		hosts = HostFileParser.extractXMLHostsInObjects();
		
		comboBoxDataProvider = new String[ hosts.size()+comboBoxDataProviderSize];
		comboBoxDataProvider[0] = PropertyManager.properties.getProperty("comboboxDefaultTxt");
		for (int i = 0; i < hosts.size(); i++) {
			System.out.println("test: "+hosts.get(i));
			comboBoxDataProvider[i+1] = hosts.get(i).getIpAddress()+" : "+hosts.get(i).getRegistryPortNumber();
		}
	}
	
	public void startAllSavedRTDG() {
		for (int i = 0; i < hosts.size(); i++) {
			try {
				if(rmiSender.send(AdminRequestType.PING,  hosts.get(i).getIpAddress(),  hosts.get(i).getRegistryPortNumber())!=-1){
					ServiceManager.printLog(PropertyManager.properties.getProperty("svcMgrAlreadyRunning1")+" "+ hosts.get(i).getIpAddress()+":"+ hosts.get(i).getRegistryPortNumber()+" "+PropertyManager.properties.getProperty("svcMgrAlreadyRunning2"));
				}else{
					StartupManager startupManager = new StartupManager( hosts.get(i));
					startupManager.startService();
				}
			} catch (RemoteException e) {
			}
		}
	}

	public void startRTDG(Host host) {
		try {
			if(rmiSender.send(AdminRequestType.PING, host.getIpAddress(), host.getRegistryPortNumber())!=-1){
				ServiceManager.printLog(PropertyManager.properties.getProperty("svcMgrAlreadyRunning1")+" "+host.getIpAddress()+":"+host.getRegistryPortNumber()+" "+PropertyManager.properties.getProperty("svcMgrAlreadyRunning2"));
			}else{
				StartupManager startupManager = new StartupManager(host);
				startupManager.startService();
			}
		} catch (RemoteException e) {
		}
	}

	public void stopRTDG(Host host) {
		try {
			rmiSender.send(AdminRequestType.STOP, host.getIpAddress(), host.getRegistryPortNumber());
		} catch (RemoteException e) {
			ServiceManager.printLog(PropertyManager.properties.getProperty("svcMgrStopError"));
		}
	}
	
	public void discover() {
		try {
			discoveryDispatcher.discover();
		} catch (IOException e) {
			ServiceManager.printLog(PropertyManager.properties.getProperty("svcMgrDiscoveryError"));
			e.printStackTrace();
		}
	}
	
	public void addNewHost(Host host, boolean start, boolean saveInFile){
		if(saveInFile){
			System.out.println("DEBUT DU SAVE");
			saveNewHost(host);
			hosts.add(host);
			comboBoxDataProvider[hosts.size()] = hosts.get(hosts.size()-1).getIpAddress()+":"+hosts.get(hosts.size()-1).getRegistryPortNumber();
			System.out.println("FIN DU SAVE");
		}
		
		if(start){
			System.out.println("DEBUT DU START");
			startRTDG(host);
			System.out.println("FIN DU START");
		}
	}
	
	public void saveNewHost(Host host){
		HostFileParser.setHostInXmlFile(host);
	}
	
	public void removeHostInFile(int index){
		HostFileParser.removeHostInXmlFile((2*(index-1))+1);
		hosts.remove(index-1);
		comboBoxDataProvider = new String[ hosts.size()+comboBoxDataProviderSize];
		comboBoxDataProvider[0] = PropertyManager.properties.getProperty("comboboxDefaultTxt");
		for (int i = 0; i < hosts.size(); i++) {
			comboBoxDataProvider[i+1] = hosts.get(i).getIpAddress()+":"+hosts.get(i).getRegistryPortNumber();
		}
	}

}
